#!/usr/bin/python
# coding=utf-8

import sys, datetime, json, logging

from xpy3lib.utils import db_utils as util

from sicost.config import app_config
from xpy3lib.XLogger import XLogger
from xpy3lib.utils.XUtils import XUtils

from sicost.DB22MPPJob import DB22MPPJob

profile = 'dev'


def db22mpp(source_schema, source_table, target_schema, target_table, account_period_start, account_period_end):
    status = 0
    try:
        info = XUtils.get_git_info()
    except Exception as e:
        info = {'message': 'no_git'}

    level = logging.ERROR if profile == 'prod' else logging.NOTSET
    XLogger.getInstance(p_level=level, p_name_key_word='sicost').logger.error(
        '\n%s' % json.dumps(info, sort_keys=True, indent=4))

    config = app_config[profile]

    try:
        db_conn_mpp = util.getConnectionDb2(config.DB_HOST_MPP_DB2,
                                            config.DB_PORT_MPP_DB2,
                                            config.DB_DBNAME_MPP_DB2,
                                            config.DB_USER_MPP_DB2,
                                            config.DB_PASSWORD_MPP_DB2)
        XLogger.getInstance().logger.info('connect db_conn_mpp success')
        db_conn_rds = util.getConnectionDb2(config.DB_HOST_RDS_DB2,
                                            config.DB_PORT_RDS_DB2,
                                            config.DB_DBNAME_RDS_DB2,
                                            config.DB_USER_RDS_DB2,
                                            config.DB_PASSWORD_RDS_DB2)
        XLogger.getInstance().logger.info('connect db_conn_rds success')

        db_conn_dbprod7 = util.getConnectionDb2(config.DB_HOST_DBPROD7_DB2,
                                            config.DB_PORT_DBPROD7_DB2,
                                            config.DB_DBNAME_DBPROD7_DB2,
                                            config.DB_USER_DBPROD7_DB2,
                                            config.DB_PASSWORD_DBPROD7_DB2)
        XLogger.getInstance().logger.info('connect db_conn_dbprod7 success')
    except Exception as e:
        db_conn_mpp = None
        db_conn_rds = None
        db_conn_dbprod7 = None
        XLogger.getInstance().logger.critical(str(e))

    try:
        run_db22mpp(p_config=config, p_db_conn_mpp=db_conn_mpp, p_db_conn_rds=db_conn_rds, p_db_conn_dbprod7=db_conn_dbprod7,
                    p_source_schema=source_schema,
                    p_source_table=source_table, p_target_schema=target_schema, p_target_table=target_table,
                    p_account_period_start=account_period_start, p_account_period_end=account_period_end)
    except Exception as e:
        XLogger.getInstance().logger.critical(str(e))
    # run_db22mpp(p_config=config, p_db_conn_mpp=db_conn_mpp, p_db_conn_dbprod7=db_conn_dbprod7, p_source_schema=source_schema,
    #             p_source_table=source_table, p_target_schema=target_schema, p_target_table=target_table,
    #             p_account_period_start=account_period_start, p_account_period_end=account_period_end)

    try:
        util.closeConnection(db_conn_mpp)
        util.closeConnection(db_conn_rds)
        util.closeConnection(db_conn_dbprod7)
    except Exception as e:
        XLogger.getInstance().logger.error(str(e))
        status = 1

    return status


def run_db22mpp(p_config=None, p_db_conn_mpp=None, p_db_conn_rds=None, p_db_conn_dbprod7=None, p_source_schema=None, p_source_table=None,
                p_target_schema=None, p_target_table=None, p_account_period_start=None, p_account_period_end=None):
    DB22MPPJob(p_config=p_config,
               p_db_conn_mpp=p_db_conn_mpp,
               p_db_conn_rds=p_db_conn_rds,
               p_db_conn_dbprod7=p_db_conn_dbprod7,
               p_source_schema=p_source_schema,
               p_source_table=p_source_table,
               p_target_schema=p_target_schema,
               p_target_table=p_target_table,
               p_account_period_start=p_account_period_start,
               p_account_period_end=p_account_period_end).execute()


if __name__ == '__main__':
    now = datetime.datetime.now()
    # now = datetime.datetime(year=2021, month=7, day=6, hour=0, minute=0)
    oneday = datetime.timedelta(days=1)
    prev_day = now - oneday
    prev_day2 = prev_day - oneday
    s = datetime.datetime(year=prev_day2.year, month=prev_day2.month, day=prev_day2.day, hour=20, minute=0,
                          second=0)
    e = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=20, minute=0, second=0)
    # account_period_start = s.strftime('%Y%m%tmp_dict%H%M%S')
    # account_period_end = e.strftime('%Y%m%tmp_dict%H%M%S')
    account_period_start = '20210101000000'
    account_period_end = '20211101000000'
    # start = datetime.datetime.now()
    # p_day_1 = (start - datetime.timedelta(days=1)).strftime("%Y%m%tmp_dict")
    # p_day_2 = (start - datetime.timedelta(days=2)).strftime("%Y%m%tmp_dict")
    # db22mpp('BGRAGGCB', 'SU_AJBG_MO0101', 'BGTAMAS1', 'T_ADS_FACT_SICB_MO0101', account_period_start,
    #         account_period_end)
    db22mpp('BGRAGGCB', 'SU_AJBG_BZSJCB_0', 'BGTARAS1', 'T_ADS_FACT_SICB_BZSJCB_0', account_period_start,
            account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CA0101', 'BGTAMAS1', 'T_ADS_FACT_SICB_CA0001', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CD0101', 'BGTAMAS1', 'T_ADS_FACT_SICB_CD0101', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CD0102', 'BGTAMAS1', 'T_ADS_FACT_SICB_CD0102', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CD0103', 'BGTAMAS1', 'T_ADS_FACT_SICB_CD0103', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CD0104', 'BGTAMAS1', 'T_ADS_FACT_SICB_CD0104', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CD0105', 'BGTAMAS1', 'T_ADS_FACT_SICB_CD0105', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CD0106', 'BGTAMAS1', 'T_ADS_FACT_SICB_CD0106', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_CD0201', 'BGTAMAS1', 'T_ADS_FACT_SICB_CD0201', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_DP0102', 'BGTAMAS1', 'T_ADS_FACT_SICB_DP0102', account_period_start,
    #         account_period_end)
    # db22mpp('BGRAGGCB', 'SU_AJBG_DP0103', 'BGTAMAS1', 'T_ADS_FACT_SICB_DP0103', account_period_start,
    #         account_period_end)

    elapsed = float((datetime.datetime.now() - now).seconds)
    print("Time Used 4 All ----->>>> %f seconds" % (elapsed))
